﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using OpenComposite.Base.Collections;
using System.Workflow.ComponentModel;
using OpenComposite.Workflow;
using OpenComposite.Workflow.Interfaces;

namespace OpenComposite.EII.Designer.Workflow.Forms
{
	public partial class FormAssignCorrelationId : Form
	{
		public FormAssignCorrelationId(Activity target, List<Activity> activities)
		{
			InitializeComponent();
			_items = new BindingListEx<BindableItem>();
			foreach ( Activity act in activities ) {
				if ( act == target ) continue;
				if ( act is LogicalAsyncStartActivity ) {
					_items.Add(new BindableItem(act));
				}
			}
			this.DialogResult = DialogResult.None;
			dgvCorrelationIds.AutoGenerateColumns = false;
			dgvCorrelationIds.DataSource = _items;
		}

		private BindingListEx<BindableItem> _items = null;
		public string SelectedCorrelationActivity
		{
			get
			{
				return getSelectedId();
			}
			set
			{
				setSelectedId(value);
			}
		}

		private void tsbtnSaveClose_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private string getSelectedId()
		{
			foreach ( DataGridViewRow row in dgvCorrelationIds.Rows ) {
				BindableItem rowId = row.DataBoundItem as BindableItem;
				if ( rowId.Checked ) return rowId.Name;
			}
			return null;
		}

		private void setSelectedId(BindableItem value)
		{
			setSelectedId(value.Name);
		}
		private void setSelectedId(string value)
		{
			foreach ( DataGridViewRow row in dgvCorrelationIds.Rows ) {
				BindableItem id = row.DataBoundItem as BindableItem;
				id.Checked = id.Name == value;
			}
		}

		private void dgvCorrelationIds_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			if ( e.RowIndex < 0 ) return;
			if ( e.ColumnIndex == colChecked.Index ) {
				bool value = (bool)dgvCorrelationIds[e.ColumnIndex, e.RowIndex].Value;
				if ( value ) {
				}
			}
		}

		private void dgvCorrelationIds_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			if ( e.RowIndex < 0 ) return;
			if ( e.ColumnIndex == colChecked.Index ) {
				BindableItem id = dgvCorrelationIds.Rows[e.RowIndex].DataBoundItem as BindableItem;
				if ( id == null ) return;
				id.Checked = !id.Checked;
				if ( id.Checked )
					setSelectedId(id);
				dgvCorrelationIds.InvalidateColumn(colChecked.Index);
			}
		}

		protected class BindableItem
		{
			public BindableItem(Activity act)
			{
				this.Activity = act;
				this.Checked = false;
			}
			public bool Checked { get; set; }
			public Activity Activity { get; set; }
			public string Text
			{
				get
				{
					ITextActivity textAct = Activity as ITextActivity;
					if ( textAct == null ) return null;
					object val = Activity.GetValue(textAct.GetTextProperty());
					return val as string;
				}
			}
			public string Name
			{
				get
				{
					if ( Activity == null ) return null;
					return Activity.Name;
				}
			}
			public Image Image
			{
				get
				{
					if ( Activity == null ) return null;
					ToolboxBitmapAttribute attr = Activity.GetType().GetAttribute<ToolboxBitmapAttribute>(false);
					if ( attr == null ) return null;
					return attr.GetImage(Activity, false);//.GetThumbnailImage(16, 16, null, IntPtr.Zero);
				}
			}
		}

		private void dgvCorrelationIds_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
		{
			if ( e.RowIndex < 0 ) return;
			if ( e.ColumnIndex != colName.Index ) return;
			e.PaintBackground(e.ClipBounds, e.State == DataGridViewElementStates.Selected);
			BindableItem item = dgvCorrelationIds.Rows[e.RowIndex].DataBoundItem as BindableItem;
			if ( item == null ) return;
			Point imagePoint = e.CellBounds.Location;
			imagePoint.Offset(2, 2);
			e.Graphics.DrawImage(item.Image, imagePoint);
			RectangleF textRect = e.CellBounds;
			textRect.Width -= item.Image.Width;
			textRect.X += item.Image.Width+4;
			StringFormat format = new StringFormat();
			format.LineAlignment = StringAlignment.Center;
			e.Graphics.DrawString(e.Value as string,
				e.CellStyle.Font,
				new SolidBrush(e.CellStyle.ForeColor),
				textRect,format);
			e.Handled = true;
		}
	}
}
